<template>
  <div id="divml1">
    <el-form :inline="true" :model="formInline" class="demo-form-inline" id="el-form1">
        <el-form-item label="">
          <el-input
            v-model="search"
            placeholder="搜索绑定会员"></el-input>
        </el-form-item>
        <el-button type="primary"><router-link tag="span" to="/memberc/membercardadd">添加会员卡</router-link></el-button>
    </el-form>

    <!-- -----------表格--------------- -->
    <el-table
      ref="multipleTable"
      :data="tableData.filter(data => !search || data.userName.toLowerCase().includes(search.toLowerCase()))"
      tooltip-effect="dark"
      style="width: 100%"
      :header-cell-style="{ textAlign: 'center', background: '#f5f7fa' }"
      :cell-style="{ textAlign: 'center' }"
      @selection-change="handleSelectionChange"
      :row-key='getRowKey'
      border>
      <el-table-column prop="name" label="会员卡"> </el-table-column>
      <el-table-column prop="userName" label="绑定会员"> </el-table-column>
      <el-table-column prop="phone" label="手机号"></el-table-column>
      <el-table-column prop="balance" label="余额"></el-table-column>
      <el-table-column label="状态">
         <template slot-scope="scope">
          {{scope.row.status==1?"启用":"停用"}}
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
        <el-button type="text" size="small" @click.native.prevent="renew1(scope.$index, tableData)">续费</el-button>
        <el-button type="text" size="small" @click.native.prevent="renew2(scope.$index, tableData)">扣费</el-button>
        <el-button type="text" size="small" @click.native.prevent="del(scope.$index, tableData)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- -------分页------------ -->
    <el-pagination 
      background 
      layout="prev,pager,next" 
      :total="total" 
      :page-size="size"  
      @current-change="change"
      @prev-click="prev"
      @next-click="next">
    </el-pagination>

    <!-- 会员卡续费 -->
    <el-dialog title="续费" :visible.sync="dialogFormVisible1">
      <el-form :model="form">
        <div id="mgespan">
          <span style="color: red">*</span>续费金额：
          <el-input placeholder="请输入内容" v-model="form.money" style="width:220px">
            <template slot="append">元</template>
          </el-input>
        </div>
         <p id="mgespanp" style="line-height:40px">
          支付方式： 
          <el-form :inline="true" :model="formInline" class="demo-form-inline" style="display:inline">
            <el-form-item>
              <el-select v-model="formInline.method" placeholder="请选择" >
                <el-option label="支付宝" value="支付宝"></el-option>
                <el-option label="微信" value="微信"></el-option>
                <el-option label="线下支付" value="线下支付"></el-option>
                <el-option label="网银" value="网银"></el-option>
              </el-select>
            </el-form-item>
          </el-form>
        </p>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible1 = false">取 消</el-button>
        <el-button type="primary" @click="confirm">确 定</el-button>
      </div>
    </el-dialog>

    <!-- 会员卡扣费 -->
    <el-dialog title="扣费" :visible.sync="dialogFormVisible2">
      <el-form :model="form">
        <div id="mgespan">
          <span style="color: red">*</span>扣除余额：
            <el-input placeholder="请输入内容" v-model="form.money" style="width:220px">
            <template slot="append">元</template>
          </el-input>
        </div>
        <div id="mgespan">
          &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: red">*</span>备注：
          <el-input placeholder="请输入内容" v-model="form.discount" style="width:220px">
          </el-input>
        </div>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible2 = false">取 消</el-button>
        <el-button type="primary" @click="fee">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      search: '',
      id:'',
      page:1,
      size:5,
      total:0,
      arr:[],
      dialogFormVisible1: false,
      dialogFormVisible2: false,
      formInline: {  //续费输入值
        method: '',
        place: '',
        userid: "",
        regionplace:"",
      },
      form: {   //续费输入值
        money:'',
        discount:"",
      },
      tableData: [],
      multipleSelection: [], //列表
    };
  },
  
  methods: {
    confirm(){  //续费接口
      this.$axios({
        url:'/gym/card/change',
        method:"post",
        data:{
          id:this.id,
          balance:this.form.money,
        }
      }).then((res)=>{
        this.getCon()
        this.dialogFormVisible1 = false
      })
    }, 

    fee(){  //扣费接口
      this.$axios({
        url:'/gym/card/change',
        method:"post",
        data:{
          id:this.id,
          balance:-this.form.money,
        }
      }).then((res)=>{
        this.getCon();
        this.dialogFormVisible2 = false
      })
    },

    renew1(index, data){  //点击续费赋值对应id
      this.dialogFormVisible1 = true;
      this.id=data[index].id;
    },

    renew2(index, data){  //点击续费赋值对应id
      this.dialogFormVisible2 = true;
      this.id=data[index].id;
    },

    getCon() {
      this.$axios({
        url: "/gym/card/list",
        params: {
          page:this.page,
          limit:this.size,
        },
      }).then((res) => {
        this.tableData = res.data.data;
        this.total = res.data.count;
        this.arr=res.data.data.map(item=>
        item.status)
      });
    },//查询页面

    getRowKey(row){
      return row.id
    },

    del(index, data){  //删除接口
      this.$confirm("此操作将永久这条内容？", "提示",{
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(()=>{
          this.$axios({
            url: "/gym/card/delete",
            method: "post",
            data: {
              id: data[index].id,
            },
        }).then((res)=>{
            if (res.data.error) {
              this.$message.error("删除失败");
        }else{
            this.$message({
              type: "success",
              message: "删除成功!",
        });
          this.getCon();
        }
      });
    })
      .catch(() => {
        this.$message({
          type: "info",
          message: "已取消删除",
        });
      });
    },//删除

    change(page) {
      this.page = page;
      this.getCon();
    },

    prev() {
      this.page--;
      this.getCon();
    },

    next() {
      this.page++;
      this.getCon();
    },//上一页下一页

    notarize() {
      console.log("submit!");
    },

    toggleSelection(rows){
      if (rows){
        rows.forEach((row)=>{
          this.$refs.multipleTable.toggleRowSelection(row);
        });
      }else{
        this.$refs.multipleTable.clearSelection();
      }
    },
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
  }, //列表
  
  mounted(){
    this.getCon();
  },
};
</script>
<style scoped>
/* 搜索栏 */
.el-table {
  text-align: center;
}

#el-button{
  float:right;
  margin-bottom: 10px;
  background: white;
  color: #b3b3b3;
  border: 1px solid #d3dce6;
}

#select{
  width: 250px;
}

.divml {
  float: left;
}

#divml1 {
  margin: 20px;
}

#mgespan{
  margin: 0px 0px 30px -20px;
}

#el-form1 {
  margin-top: 20px;
  float: left;
}

.el-select {
  margin-right: 10px;
}

.el-pagination {
  float: right;
  margin-top: 20px;
}

#mgespaninput{
  width: 250px;
}

#mgespanp{
  margin: 10px;
}
</style>
